//
//  GameManager.m
//  EasyToo
//
//  Created by zhengzhe on 13-8-25.
//
//

#import "GameManager.h"
#import "ServerCommunicationManager.h"
#import "GameCountSearchRequest.h"
#import "GameCountSearchResponse.h"
#import "ChargeGameRequest.h"
#import "ChargeGameResponse.h"
#import "CheckGameChargeInfoRequest.h"
#import "CheckGameChargeInfoResponse.h"

@implementation GameManager

static GameManager* gManager = nil;


+ (GameManager*)shareManager
{
    if (gManager == nil) {
        gManager = [[GameManager alloc] init];
    }
    return gManager;
}

- (void)searchAllGame
{
    GameCountSearchRequest *request = [[GameCountSearchRequest alloc] initWithGameType:@"1" Delegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];
}

- (void)chargeGameWithId:(NSString *)gameId gameUser:(NSString *)gameUser cardNum:(NSString *)cardNum
{
    ChargeGameRequest *request = [[ChargeGameRequest alloc] initWithGameId:gameId andGameUser:gameUser andCardNum:cardNum Delegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];
}

- (void)checkGameChargeInfo:(NSString *)gameId gameName:gameName
{
    CheckGameChargeInfoRequest *request = [[CheckGameChargeInfoRequest alloc] initWithGameId:gameId andGameUser:gameName Delegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];
}



- (void)onRequestSuccess:(BaseResponse *)response
{
    if ([response isKindOfClass:[GameCountSearchResponse class]])
    {
        [self handleGameCountSearchSuccess:(GameCountSearchResponse*)response];
    }
    else if ([response isKindOfClass:[ChargeGameResponse class]])
    {
        [self handleGameChargeSuccess:(ChargeGameResponse*)response];
    }
    else if ([response isKindOfClass:[CheckGameChargeInfoResponse class]])
    {
        [self handleCheckGameChargeInfoSuccess:(CheckGameChargeInfoResponse*)response];
    }

}

- (void)onRequestFail:(BaseResponse *)response
{
    if ([response isKindOfClass:[GameCountSearchResponse class]])
    {
        [self.delegate onSearchAllGameFinish:NO Games:nil];
    }
    else if ([response isKindOfClass:[ChargeGameResponse class]])
    {
        [self.delegate onChargeGameFinish:NO tn:nil];
    }
}

- (void)handleGameCountSearchSuccess:(GameCountSearchResponse*)response
{
    NSMutableArray *gameInfoArray = response.gameInfoArray;
    if (gameInfoArray)
    {
        [self.delegate onSearchAllGameFinish:YES Games:gameInfoArray];
    }
    else
    {
        [self.delegate onSearchAllGameFinish:NO Games:nil];
    }
}

- (void)handleCheckGameChargeInfoSuccess:(CheckGameChargeInfoResponse*)response
{
    NSMutableArray *gameChargeInfoArray = response.cardInfoArray;
    if (gameChargeInfoArray)
    {
        [self.delegate onCheckGameChargeInfoFinish:YES gameInfo:gameChargeInfoArray];
        
    }
    else
    {
        [self.delegate onCheckGameChargeInfoFinish:NO gameInfo:nil];
    }
}


- (void)handleGameChargeSuccess:(ChargeGameResponse*)response
{
    NSString *tn = response.tn;
    if (tn)
    {
        [self.delegate onChargeGameFinish:YES tn:tn];
    }
    else
    {
        [self.delegate onChargeGameFinish:NO tn:nil];
    }
}


@end
